22. Practice Questions
Practice Questions
The following questions are based on the same text you saw in the last lesson, the first verse of the poem If by Rudyard Kipling. We've converted all letters to lowercase, removed punctuation marks from the text, and stored this modified text in the string variable verse
.
Quiz: Count Unique Words
Your task for this quiz is to find the number of unique words in the text. In the code editor below, complete these three steps to get your answer.
- Split
verse
into a list of words. Hint: You can use a string method you learned in the previous lesson. - Convert the list into a data structure that would keep only the unique elements from the list.
- Print the length of the container.
Start Quiz:
verse = "if you can keep your head when all about you are losing theirs and blaming it on you if you can trust yourself when all men doubt you but make allowance for their doubting too if you can wait and not be tired by waiting or being lied about don’t deal in lies or being hated don’t give way to hating and yet don’t look too good nor talk too wise"
print(verse, '\n')
# split verse into list of words
verse_list =
print(verse_list, '\n')
# convert list to a data structure that stores unique elements
verse_set =
print(verse_set, '\n')
# print the number of unique words
num_unique =
print(num_unique, '\n')
Quiz: Verse Dictionary
In the code editor below, you'll find a dictionary containing the unique words of verse
stored as keys and the number of times they appear in verse
stored as values. Use this dictionary to answer the following questions. Submit these answers in the quiz below the code editor.
Try to answer these using code, rather than inspecting the dictionary manually!
- How many unique words are in verse_dict?
- Is the key "breathe" in verse_dict?
- What is the first element in the list created when verse_dict is sorted by keys?
Hint: Use the appropriate dictionary method to get a list of its keys, and then sort that list. Use this list of keys to answer the next two questions as well. - Which key (word) has the highest value in verse_dict?
Start Quiz:
verse_dict = {'if': 3, 'you': 6, 'can': 3, 'keep': 1, 'your': 1, 'head': 1, 'when': 2, 'all': 2, 'about': 2, 'are': 1, 'losing': 1, 'theirs': 1, 'and': 3, 'blaming': 1, 'it': 1, 'on': 1, 'trust': 1, 'yourself': 1, 'men': 1, 'doubt': 1, 'but': 1, 'make': 1, 'allowance': 1, 'for': 1, 'their': 1, 'doubting': 1, 'too': 3, 'wait': 1, 'not': 1, 'be': 1, 'tired': 1, 'by': 1, 'waiting': 1, 'or': 2, 'being': 2, 'lied': 1, 'don\'t': 3, 'deal': 1, 'in': 1, 'lies': 1, 'hated': 1, 'give': 1, 'way': 1, 'to': 1, 'hating': 1, 'yet': 1, 'look': 1, 'good': 1, 'nor': 1, 'talk': 1, 'wise': 1}
print(verse_dict, '\n')
# find number of unique keys in the dictionary
num_keys =
print(num_keys)
# find whether 'breathe' is a key in the dictionary
contains_breathe =
print(contains_breathe)
# create and sort a list of the dictionary's keys
sorted_keys =
# get the first element in the sorted list of keys
print()
# find the element with the highest value in the list of keys
print()
QUIZ QUESTION::
Match the correct answer to the following questions about the dictionary, verse_dict
.
ANSWER CHOICES:
Question |
Response |
---|---|
1 |
|
NO |
|
'wise' |
|
'about' |
|
'and' |
|
'if' |
|
There is no first key. |
|
50 |
|
51 |
|
YES |
|
'yourself' |
SOLUTION:
Question |
Response |
---|---|
NO |
|
'about' |
|
There is no first key. |
|
51 |
|
'yourself' |